package com.meixiang.activity.homes.service;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lzy.okhttputils.model.HttpParams;
import com.meixiang.R;
import com.meixiang.adapter.service.ServiceContentAdapter;
import com.meixiang.adapter.service.ServiceProjectAdapter;
import com.meixiang.entity.services.ServiceDetail;
import com.meixiang.global.Config;
import com.meixiang.global.GlobalType;
import com.meixiang.http.HttpCallBack;
import com.meixiang.http.HttpUtils;
import com.meixiang.main.BaseActivity;
import com.meixiang.tool.Tool;
import com.meixiang.util.AbJsonUtil;
import com.meixiang.util.AbStrUtil;
import com.meixiang.util.AbToastUtil;
import com.meixiang.view.OldPriceView;
import com.meixiang.view.ProjectView;

import org.json.JSONObject;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 预约--项目--服务详情
 * xiejingwen
 */
public class ServiceDetailActivity extends BaseActivity {

    @Bind(R.id.iv_banner)
    ImageView ivBanner;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_now_price)
    TextView tvNowPrice;
    @Bind(R.id.tv_outmoded_price)
    OldPriceView tvOutmodedPrice;
    @Bind(R.id.tv_people_number)
    TextView tvPeopleNumber;
    @Bind(R.id.tv_time_consuming)
    TextView tvTimeConsuming;
    @Bind(R.id.tv_introduce_content)
    TextView tvIntroduceContent;
    @Bind(R.id.tv_check_evaluate)
    TextView tvCheckEvaluate;
    @Bind(R.id.tv_step)
    TextView tvStep;
    @Bind(R.id.rv_content)
    RecyclerView rvContent;
    @Bind(R.id.btn_reservation)
    Button btnReservation;
    @Bind(R.id.left_image_btn)
    ImageView leftImageBtn;
    @Bind(R.id.tv_kefu)
    ImageView tvKefu;
    @Bind(R.id.iv_collect)
    ImageView ivCollect;
    @Bind(R.id.view_line)
    View viewLine;
    @Bind(R.id.ll_content)
    LinearLayout llContent;
    @Bind(R.id.projiect_introduce)
    ProjectView projiectIntroduce;
    @Bind(R.id.rv_prolist)
    RecyclerView rvProlist;
    private ServiceContentAdapter mAdapter;
    private ServiceProjectAdapter proAdapter;
    private ServiceDetail detail;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_service_detail);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initTitle() {

    }

    @Override
    protected void initView() {
        rvContent.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
        mAdapter = new ServiceContentAdapter(this);
        rvContent.setAdapter(mAdapter);

        rvProlist.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        proAdapter=new ServiceProjectAdapter(this);
        rvProlist.setAdapter(proAdapter);
    }

    @Override
    protected void setData() {
        status.showLoading(llContent);
        getData();
    }

    @OnClick({R.id.left_image_btn, R.id.tv_kefu, R.id.iv_collect, R.id.tv_check_evaluate, R.id.btn_reservation})
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.left_image_btn:
                finish();
                break;
            case R.id.tv_kefu:
                if(detail!=null&&!AbStrUtil.isEmpty(detail.getIsMainStore())){
                    if (detail.getIsMainStore().equals("1")) {
                        Tool.SelectCustomerService(context, true);
                    } else {
                        Tool.SelectCustomerService(context, false);
                    }
                }else{
                    Tool.SelectCustomerService(context, false);
//                    Tool.showTextToast(context,"未获取信息");
                }
                break;
            case R.id.iv_collect:
                if(detail!=null&&!AbStrUtil.isEmpty(detail.getOpera())){
                    if (detail.getOpera().equals("1")) {//已收藏
                        addFavorite("2", detail.getGoodsId());
                    } else {
                        addFavorite("1", detail.getGoodsId());
                    }
                }else{
                    Tool.showTextToast(context,"未获取信息");
                }
                break;
            case R.id.tv_check_evaluate:
                Bundle evaluate = new Bundle();
                evaluate.putString("o2oServiceId", detail.getGoodsId());
                evaluate.putString("type", GlobalType.SERVICE_EVALUATE_TYPE);
                startActivity(evaluate, ServiceEvaluateActivity.class);
                break;
            case R.id.btn_reservation:
                Intent intent = new Intent();
                intent.setClass(ServiceDetailActivity.this, ReservationActivity.class);
                intent.putExtra("type",GlobalType.ADD_RESERVATION);
                intent.putExtra("goodsId", detail.getGoodsId());
                intent.putExtra("price", detail.getGoodsStorePrice());
                intent.putExtra("isMainStore", detail.getIsMainStore());
                intent.putExtra("goodsBody", detail.getGoodsName());
                intent.putExtra("goodsImage", detail.getGoodsImage());
                startActivity(intent);
                break;
        }
    }

    /**
     * @param opera   类型 1 为收藏 2为取消收藏
     * @param goodsId
     */
    private void addFavorite(final String opera, String goodsId) {
        HttpParams params = new HttpParams();
        params.put("opera", opera);
        params.put("goodsId", goodsId);
        HttpUtils.post(Config.SERVICE_FAVORITE, params, new HttpCallBack(this) {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                AbToastUtil.showToast(activity, message);
                getData();
            }

            @Override
            public void onError(String code, String message) {
                AbToastUtil.showToast(activity, message);
            }
        });
    }



    private void getData() {
        HttpParams params = new HttpParams();
        params.put("goodsId", getIntent().getStringExtra("goodsId"));
        HttpUtils.post(Config.SERVICE_DETAIL, params, new HttpCallBack(this) {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                status.removeView();
                detail = AbJsonUtil.fromJson(result.toString(), ServiceDetail.class);
                if (detail.getOpera().equals("1")) {//已收藏
                    ivCollect.setImageResource(R.mipmap.collect);
                } else {
                    ivCollect.setImageResource(R.mipmap.collect_2);
                }
                Glide.with(activity)
                        .load(detail.getGoodsImage())
                        .fitCenter()
                        .placeholder(R.drawable.image_default_gray_bg)
                        .error(R.drawable.image_default_gray_bg)
                        .into(ivBanner);
                final ArrayList<String> urls = new ArrayList<>();
                urls.add(detail.getGoodsImage());
                ivBanner.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(activity, ShowPictureActivity.class);
                        intent.putStringArrayListExtra("urls", urls);
                        startActivity(intent);
                    }
                });
                tvTitle.setText(detail.getGoodsName());
                if (!AbStrUtil.isEmpty(detail.getGoodsStorePrice())) {
                    if (Double.parseDouble(detail.getGoodsStorePrice()) >= 1) {
                        tvNowPrice.setText((int) Double.parseDouble(detail.getGoodsStorePrice()) + "");
                    } else {
                        tvNowPrice.setText(detail.getGoodsStorePrice());
                    }
                } else {
                    tvNowPrice.setText("0");
                }
                if (!AbStrUtil.isEmpty(detail.getGoodsMarketPrice())) {
                    if (Double.parseDouble(detail.getGoodsStorePrice()) >= 1) {
                        tvOutmodedPrice.setText("¥ " + (int) Double.parseDouble(detail.getGoodsMarketPrice()) + "");
                    } else {
                        tvOutmodedPrice.setText("¥ " + detail.getGoodsMarketPrice());
                    }

                } else {
                    tvOutmodedPrice.setText("¥ 0");
                }
                tvPeopleNumber.setText(detail.getSalenum() + "人做过");
                tvTimeConsuming.setText("耗时 " + detail.getServiceTime() + "分钟");
                if (AbStrUtil.isEmpty(detail.getGoodsBody())) {
                    tvIntroduceContent.setText("暂无描述");
                } else {
                    tvIntroduceContent.setText(detail.getGoodsBody());
                }
                mAdapter.setStepListBean((ArrayList<ServiceDetail.StepListBean>) detail.getStepList());
                tvStep.setText("服务内容(" + detail.getStepList().size() + "个步骤)");
                if (detail.getProList().size() > 0) {
                    proAdapter.addData(detail.getProList());
                }

                projiectIntroduce.setData(detail.getInstructionList());

            }

            @Override
            public void onError(String code, String message) {
                AbToastUtil.showToast(context, message);
                status.showNoData(llContent, message, "重新加载", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        status.showLoading(llContent);
                        getData();
                    }
                });
            }

        });
    }

}
